package cn.wildfire.chat.app.extendsion.classes;

import android.content.Context;

import org.json.JSONArray;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import cn.wildfire.chat.app.extendsion.https.OkHttpRequest;
import cn.wildfire.chat.app.extendsion.https.ReqCallBack;
import cn.wildfire.chat.app.extendsion.https.ServerUrlClient;
import cn.wildfire.chat.app.extendsion.utils.TextUtils;
import cn.wildfirechat.model.GroupMember;

/**
 * Create by HEKE, 2019/8/25 20:01
 * TODO 获取群组信息
 */
public class GroupMembersByApiRequest {

    private Context context;
    private String groupId;
    private OnGetGroupMembersListener onGetGroupMembersListener;

    public GroupMembersByApiRequest(Context context, String groupId, OnGetGroupMembersListener onGetGroupMembersListener) {
        this.context = context;
        this.groupId = groupId;
        this.onGetGroupMembersListener = onGetGroupMembersListener;
        req();
    }

    private void req() {
        try {
            HashMap<String, Object> params = new HashMap<>();
            params.put("gid", groupId);
            OkHttpRequest.getNewInstance(context).requestAsynJsonObject(ServerUrlClient.GET_GROUPMEMBER_INFO, params, new ReqCallBack<Object>() {
                @Override
                public void onReqSuccess(Object result) {
                    try {
                        String strRet = result.toString();
                        if (!TextUtils.isTextEmpty(strRet)) {
                            ResponseEntity<JSONArray, ArrayList<ApiGroupMember>> responseEntity = new ResponseEntity<>(strRet);
                            if (responseEntity.isError()) {
                                nullRet();
                            } else {
                                ArrayList<ApiGroupMember> apiGroupMemberArrayList = responseEntity.parse(ApiGroupMember.class);
                                List<GroupMember> groupMemberList = new ArrayList<>();
                                if (apiGroupMemberArrayList != null && apiGroupMemberArrayList.size() > 0) {
                                    for (ApiGroupMember apiGroupMember : apiGroupMemberArrayList) {
                                        groupMemberList.add(apiGroupMember.exchange(groupId));
                                    }
                                    if (onGetGroupMembersListener != null) {
                                        onGetGroupMembersListener.result(groupMemberList);
                                    }
                                } else {
                                    nullRet();
                                }
                            }
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }

                @Override
                public void onReqFailed(String errorMsg) {
                    nullRet();
                }
            }).request();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 空结果
     */
    private void nullRet() {
        if (onGetGroupMembersListener != null)
            onGetGroupMembersListener.result(null);
    }

    /**
     * Create by HEKE, 2019/8/25 20:04
     * TODO 返回数组接口
     */
    public interface OnGetGroupMembersListener {
        void result(List<GroupMember> groupMemberList);
    }

}
